import { reactive, ref, computed } from "vue"
import _ from "lodash"
import { useUserStore } from "../../../../store/user"
import jsCookie from "js-cookie"
import { getNewsCollect, queryArticle } from "../../../../service/article"

export const usePolicyHook = (info: any) => {
  const userStore = useUserStore()
  userStore.getUserInfo()
  const userInfo = computed<any>(() => userStore.userInfo)
  const token = jsCookie.get("token")
  // 当前活跃tab
  const activeTab = ref("info")
  // 置顶资讯
  const topNews = ref<any[]>([])
  // 批次资讯
  const batchNews = ref<any[]>([])

  /**
   * 查询表单
   */
  interface NewsFormType {
    title: string
    proof: string
    startTime: string
    endTime: string
    batchs: string[]
    batchs2?: string
    characters: string[]
  }
  const newsForm = reactive<NewsFormType>({
    title: "",
    proof: "",
    startTime:'',
    endTime: "",
    batchs: [],
    batchs2: "",
    characters: []
  })

  const newsLoading = ref(false)

  // 资讯性质数据
  const characters = ref([
    { dictLabel: "政策", dictValue: "8" },
    { dictLabel: "申报", dictValue: "9" },
    { dictLabel: "评审", dictValue: "10" },
    { dictLabel: "公示", dictValue: "87237" },
    { dictLabel: "立项", dictValue: "11" },
    { dictLabel: "拨款", dictValue: "12" },
    { dictLabel: "监理", dictValue: "13" },
    { dictLabel: "验收", dictValue: "14" },
    { dictLabel: "其他", dictValue: "95466" },
  ])
  const batchs = computed(()=>{
    const year = []
    const date = new Date();
      const currentYear = date.getFullYear();
      // year.push({ dictLabel: currentYear, dictValue: currentYear })
      for(var i=0;i<20;i++){
        year.push({ dictLabel: currentYear-i, dictValue: currentYear-i })
      }
    return year
  })
  // 项目批次数据
  // const batchs = ref([
  //   { dictLabel: "2024", dictValue: "2024" },
  //   { dictLabel: "2023", dictValue: "2023" },
  //   { dictLabel: "2022", dictValue: "2022" },
  //   { dictLabel: "2021", dictValue: "2021" },
  //   { dictLabel: "2020", dictValue: "2020" },
  //   { dictLabel: "2019", dictValue: "2019" },
  //   { dictLabel: "2018", dictValue: "2018" },
  //   { dictLabel: "2017", dictValue: "2017" },
  //   { dictLabel: "2016", dictValue: "2016" },
  //   { dictLabel: "2015", dictValue: "2015" },
  //   { dictLabel: "2015", dictValue: "2015" },
  // ])
  // 选择方法
  const checkTag = (val: string) => {
    // 存在当前val则是取消操作
    if (newsForm.batchs.includes(val)) {
      // 找到当前值在列表中的位置
      const relIdx = newsForm.batchs.findIndex((batch) => batch === val)

      // 删除掉当前索引
      newsForm.batchs.splice(relIdx, 1)
    } else {
      newsForm.batchs.push(val)
    }
  }

  // 查询方法
  const newsFormTitle = ref('')
  const proof = ref('')
  const topNewsTotal = ref(0)
  const queryNews = () => {
    // newsForm.startTime  = completeTime.value[0]
    // newsForm.endTime  = completeTime.value[1]
    const params = _.cloneDeep(newsForm)
    newsLoading.value = true

    // 判断当前条件中是否存在
    if (params.batchs2 && params.batchs.includes('0')) {
      params.batchs = params.batchs?.concat([params.batchs2 || "0"]).filter((item: any) => item != "0")
    } else {
      params.batchs = params.batchs?.filter((item: any) => item != "0")
    }

    delete params.batchs2
    newsFormTitle.value = newsForm.title
    proof.value = newsForm.proof
    getNewsCollect({pageNum:currentPage.value,pageSize:pageSize.value},{ ...params, policyId: info.id }).then((res: any) => {
      // topNews.value = res.data.topInformationList
      // batchNews.value = res.data.batchInformationList
      // console.log(res.data.batchInformationList);
      topNews.value = res.rows
      topNewsTotal.value = res.total
      // 无置顶
      // const topArr = res.data?.topInformationList ? res.data.topInformationList : []
      // const batchArr = res.data?.batchInformationList ? res.data.batchInformationList : []

      // topNews.value = [
      //   ...topArr?.map((item: any) => ({ type: 1, ...item })),
      //   ...batchArr?.map((item: any, index: number) => {
      //     const type = index % 2 === 0 ? 2 : 1
      //     return item.informationList?.map((subItem: any) => ({ type, ...subItem }))
      //   }).flat(),]

      // console.log(abc);
    }).finally(() => {
      newsLoading.value = false
    })
  }

  // 查询默认资讯数据
  // queryNews()

  // 重置方法
  const completeTime = ref([])
  const resetNewsForm = () => {
    newsForm.batchs = []
    newsForm.characters = []
    newsForm.batchs2 = ""
    newsForm.title = ''
    newsForm.proof = ''
    newsForm.startTime=''
    newsForm.endTime=''

    queryNews()
  }

  // 当前host
  const currHost = window.location.origin


  /**
   * 数据分析图
   */
  const dataPieList = ref<any[]>([
    { type: "1", label: "金额分析" },
    { type: "2", label: "地区分析" },
    { type: "3", label: "性质分析" },
    { type: "4", label: "行业分析" },
  ])


  /**
   *
   */
  const atlasList = ref<any[]>(
    [
      { type: "1", label: "所属图谱" },
      { type: "2", label: "关联图谱" },
      { type: "3", label: "管理图谱" },
      { type: "4", label: "演变图谱" },
    ]
  )
  const tabClick =  (tab)=>{
    if(tab.index=='3'){
      queryProject()
    }else if(tab.index=='1'){
      queryNews()
    }
  }
  /**
   * 公共方法
   */
  // 打开新窗口
  const openNewPage = (path: 'analysis' | 'relationImg', type: string,seachType:String) => {
    window.open(`${location.origin}/${path}?id=${info.id}&type=${type}&seachType=`+seachType)
  }

  // 立项查询按钮
  const projectData = ref()
  const showDrawer = ref(false)
  const loading  = ref(false)
  const projectDataTotal = ref(0)

  const queryProject = (params?: any) => {
    // 查询数据
    loading.value = true
    let query = {
      ...params?.params
    }
    queryArticle({ searchType: "4", policyId: info.id, ...query }, { pageNum: params?.currentPage4?params.currentPage4:1, pageSize: params?.pageSize4? params.pageSize4:100 }).then(res => {
      projectData.value = res.rows
      projectDataTotal.value = res.total

    }).finally(() => {
      loading.value = false
    })
  }
  const pageSize = ref(100)
  const currentPage = ref(1)

  const handleSizeChange = (val: number) => {
    // console.log(`${val} items per page`)
    pageSize.value = val
    currentPage.value =1
    queryNews()
    // props.queryProject(form,pageSize4.value,currentPage4.value)
  }
  const handleCurrentChange = (val: number) => {
    // console.log(`current page: ${val}`)
    currentPage.value = val
    queryNews()

    // query({},pageSize.value,currentPage4.value)

  }
  return {
    activeTab,
    topNews,
    batchNews,
    atlasList,
    newsForm,
    characters,
    batchs,
    checkTag,
    queryNews,
    resetNewsForm,
    newsLoading,
    currHost,
    dataPieList,
    openNewPage,
    queryProject,
    showDrawer,
    projectData,
    projectDataTotal,
    userInfo,
    token,
    tabClick,
    newsFormTitle,
    proof,
    loading,
    completeTime,
    handleSizeChange,
    handleCurrentChange,
    pageSize,
    currentPage,topNewsTotal
  }
}
